<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="visible"
    :before-close="close"
    lock-scroll
    append-to-body
  >
    <el-form status-icon :rules="rules" :model="datas" @keyup.enter.native="save" size="small" ref="editForm" inline>
          <el-form-item prop="account" label="禅道账号" :label-width="baseStyle.width">
            <el-input v-model="datas.account" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="realname" label="真实姓名" :label-width="baseStyle.width">
            <el-input v-model="datas.realname" autocomplete="off"></el-input>
          </el-form-item>
        
          <el-form-item prop="nickname" label="用户昵称" :label-width="baseStyle.width">
            <el-input v-model="datas.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="mobile" label="电话号码" :label-width="baseStyle.width">
            <el-input v-model="datas.mobile" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="empuid" label="empuId" :label-width="baseStyle.width">
            <el-input v-model="datas.empuid" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="email" label="email" :label-width="baseStyle.width">
            <el-input v-model="datas.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="gender" label="用户性别" :label-width="baseStyle.width">
            <el-radio v-model="datas.gender" label="m">男</el-radio>
            <el-radio v-model="datas.gender" label="f">女</el-radio>
          </el-form-item>
          <el-form-item prop="deleted" label="状态" :label-width="baseStyle.width">
            <el-radio v-model="datas.deleted" label="0">激活</el-radio>
            <el-radio v-model="datas.deleted" label="1">禁用</el-radio>
          </el-form-item>
      
          <el-form-item prop="realname" label="角色" :label-width="baseStyle.width">
            <el-select v-model="roles" multiple style="width:100%" placeholder="请选择">
              <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.name"
                :value="item.roleId">
              </el-option>
            </el-select>
          </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button :type="'primary'" :loading="saveLoading" @click.native="save()">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { Message } from "element-ui";
import rightsSetAPI from "@apis/rightsSet";
export default {
  name: "ClassicForm",
  props: {
    visible: Boolean,
    baseStyle: Object,
    title: String,
    // datas: {
    //   type: Object,
    //   default: function () {
    //     return { account: "", realname: "", roles: [] };
    //   },
    // },
    saveForm: Function,
  },
  data() {
    const rules = this.getRules();
    return {
      saveLoading: false,
      rules,
      datas: { account: "", realname: "", roles: [] },
      roles: [],
      roleList: []
    };
  },
  computed: {},
  created() {
    rightsSetAPI.getRolesList({pageNo:1,pageSize:9999}).then(res=>{
      this.roleList = res.content
    })
  },
  methods: {
    cancel() {
      this.saveLoading = false;
      this.$emit("update:visible", false);
      this.$refs.editForm.resetFields();
    },
    setData(data){
      if(data&&data.roles){
        this.roles = data.roles.map(i=>i.roleId)
      }else{
        this.roles = []
      }
      this.datas = data
    },
    close() {
      this.cancel();
    },
    save() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.saveLoading = true;
          // let roles = []
          // this.roles.map(item=>{
          //   roles.push(...this.roleList.filter(i=>i.roleId==item))
          // })
          this.$emit("saveForm", { ...this.datas,roles:this.roles});
          // this.cancel();
        } else {
          return false;
        }
      });
    },
    getRules() {
      return {
        account: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!this.datas.account) {
                callback(new Error(`请输入禅道账号`));
                return;
              }
              if (this.datas.account.length < 1) {
                callback(new Error(`请输入禅道账号`));
                return;
              }
              callback();
            },
            trigger: "blur",
          },
        ],
        realname: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!this.datas.realname) {
                callback(new Error(`请输入真实姓名`));
                return;
              }
              if (this.datas.realname.length < 1) {
                callback(new Error(`请输入真实姓名`));
                return;
              }
              callback();
            },
            trigger: "blur",
          },
        ],
      };
    },
  }
}
</script>
<style lang="scss" scoped>
</style>